<template>
	<view>
		<!-- 商品展示区域 -->
		<view class="goods-box">
			<view class="goods-item" v-for="(item,index) in goodslist" :key="item.goods_id"			
			@click="gotodetail(item)"
			>			
					<!-- 商品左侧图片展示区域 -->
					<view class="goods-img" >
						<image :src="item.goods_small_logo" mode="" ></image>
					</view>
					<!-- 商品右侧信息描述区域 -->
					<view class="goods-detail" >
						<view class="goods-detail-text" >{{item.goods_name}}</view>
						<view class="goods-detail-charge">￥{{item.goods_price}}</view>
					</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//封装一个请求参数
				queryobj:{
					query:'',
					cid:'',
					pagenum:1,
					pagesize:10
				},
				goodslist:[],
				total:0,
				isloading:false
			};
		},
		methods:{
			async  getgoodslist(cb){
				// 请求数据之前打开节流阀
				this.isloading=true
				cb&&cb();
				let [nu,res]=await uni.request({
					url:"https://api-hmugo-web.itheima.net/api/public/v1/goods/search",
					method:"GET",
					data:{
						query:this.queryobj.query,
						cid:this.queryobj.cid,
						pagenum:this.queryobj.pagenum,
						pagesize:this.queryobj.pagesize
					}
				})
				if(res.data.meta.status!==200)
				return uni.showToast({
					title:"消息请求失败"
				});
				if(this.goodslist==[]){
					res.data.message.goods.forEach((e)=>{
						if(e.goods_small_logo!==''){
							this.goodslist.push(e)
						}
					})
				}else{
					this.goodslist=[...this.goodslist,...res.data.message.goods]
				}
				
				this.total=res.data.message.total
				console.log(this.goodslist);
				// console.log(this.total);
				// 请求数据完成之后,关闭节流阀
				this.isloading=false
			},
			// 跳转商品详情页面
			gotodetail(good){
				uni.navigateTo({
					url:"/subpckA/good_detail/good_detail?good_id="+good.goods_id
				})
			}
		},
		onLoad(options){
			// console.log(options);
			this.queryobj.query=options.query||''
			this.queryobj.cid=options.cid||''
			console.log(this.queryobj);
			this.getgoodslist();
		},
		onReachBottom(){
			// 判断是否正在请求数据
			if(this.isloading) return;
			//判断是否还有数据
			if(this.queryobj.pagenum*this.queryobj.pagesize>=this.total) return uni.showToast({
				title:"没有更多商品了",
			});
			this.queryobj.pagenum++
			this.getgoodslist()
		},
		onPullDownRefresh() {
			this.queryobj.pagenum=1
			this.total=0
			this.isloading=false
			this.goodslist=[]
			//重新发起数据请求
			this.getgoodslist(()=>{
				uni.stopPullDownRefresh()
			})
			
			
		}
	}
</script>

<style lang="scss">
.goods-box{
	.goods-item{
		display: flex;
		border: 1px solid #efefef;
		padding: 10px 5px;
		.goods-img{
			width: 100px;
			height: 100px;
			image{
			   width: 100%;
			   height: 100%;
			   display: block;
			   background-size: cover;
			}
		}
		.goods-detail{
			width: 225px;
			height: 90px;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			font-size: 13px;
			padding: 5px 8px;
			.goods-detail-charge{
				color: red;
				font-size: 16px;
			}
		}
	}
}
</style>
